$(function(){$('#mytable').datagrid({
	url:'/new12306/TicketServlet?action=query',
	pagination:true,
	width:900,
	height:500,
	rownumbers:true,
	title:'请输入查询条件',
	toolbar:'#searchtool',
	loadMsg:'数据加载中，请稍后...',
	pageSize:5,
	singleSelect:"true",
	pageList:[5,10,15,20],
	columns:[[
		{
			title:'编号',
			field:'ticketId',
			width:'100'},
			{
				title:'车次',
				field:'trainNumber',
				width:'100'},
			{
				title:'起点站',
				field:'startStation',
				width:'100'},
			{
				title:'终点站',
				field:'terminalStation',
				width:'100'},
			{
				title:'发车时间',
				field:'time',
				width:'150',
				formatter: formatDatebox},
			{
				title:'到站时间',
				field:'arrivalTime',
				width:'150',
				formatter: formatDatebox},
			{
				title:'价格',
				field:'price',
				width:'100'},
			{
				title:'余票',
				field:'remainingTickets',
				width:'100'}
							
	]]
	
}),
$("#mytable").datagrid("getPager").pagination({
	  beforePageText: '第',//页数文本框前显示的汉字 
  afterPageText: '页    共 {pages} 页', 
  displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
}),
$("#query").on("click" , function(){
	$('#mytable').datagrid();
}),
$("#search").on("click" , function(){
	var startStation=$('#startStation').val();
	var terminalStation=$('#terminalStation').val();
	var time=$('#time').val();
	$.ajax({  
		url:'/new12306/TicketServlet?action=query', 
        type: 'GET',  
        contentType:"application/json" ,
        cache: false,
        data:{"startStation":startStation,"terminalStation":terminalStation,"time":time,"page":page,"rows":rows},
        success: function(pager){
			$("#mytable").datagrid();
        }
    });
})


})









Date.prototype.format = function (format) {    
    var o = {    
        "M+": this.getMonth() + 1, // month    
        "d+": this.getDate(), // day    
        "h+": this.getHours(), // hour    
        "m+": this.getMinutes(), // minute    
        "s+": this.getSeconds(), // second    
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter    
        "S": this.getMilliseconds()    
        // millisecond    
    }    
    if (/(y+)/.test(format))    
        format = format.replace(RegExp.$1, (this.getFullYear() + "")    
            .substr(4 - RegExp.$1.length));    
    for (var k in o)    
        if (new RegExp("(" + k + ")").test(format))    
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));    
    return format;    
}    



function formatDatebox(value) {    
    if (value == null || value == '') {    
        return '';    
    }    
    var dt;    
    if (value instanceof Date) {    
        dt = value;    
    } else {    
        dt = new Date(value);    
    }    
    
    return dt.format("yyyy-MM-dd hh:mm:ss"); //扩展的Date的format方法(上述插件实现)    
}
function doSearch(){  
    $('#mytable').datagrid('load',{
    	startStation: $('#startStation').val(),
    	terminalStation:$('#terminalStation').val(),
    	time:$('#time').val()
    })
}
function doReserve(){
	 var row = $('#mytable').datagrid('getSelected');
	 if(row){
		 var ticketId=row.ticketId;
		 window.location.href="http://localhost:8080/new12306/TicketServlet?action=reserve&ticketId="+ticketId;
		
	 }else{
		 alert("请选择正确的车次!");
	 }
}
